package com.infonautics.dao.imp;

import java.util.List;

import com.hibernate.Download;
import com.hibernate.HibernateDao;
import com.infonautics.dao.IDownloadDao;
import com.infonautics.page.PageBean;
import com.system.dao.ISystemDao;

/**
 * 
 * @author 马萍
 * 体验中心dao实现类
 */
public class IDownloadDaoImp implements ISystemDao<Download>,IDownloadDao {

	HibernateDao<Download> downloadDao = new HibernateDao<Download>();
	
	/*
	 * 查询全部下载产品
	 * (non-Javadoc)
	 * @see com.infonautics.dao.IDownloadDao#doDownloadList(int, int)
	 */
	@Override
	public PageBean<Download> doDownloadList(int pageSize, int page)
			throws Exception {
		
		final String hql = "from Download order by id desc";
		int allRow = downloadDao.getAllRowCount("select count(*) from Download");
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int length = pageSize;
		final int currentPage = PageBean.countCurrentPage(page);
		
		List<Download> list = downloadDao.getObjects(hql, offset, length);
		
		PageBean<Download> pageBean = new PageBean<Download>();
		
		pageBean.setAllRow(allRow);
		pageBean.setCurrentPage(currentPage);
		pageBean.setList(list);
		pageBean.setPageSize(pageSize);
		pageBean.setTotalPage(totalPage);
		pageBean.init();
		
		return pageBean;
	}

	/*
	 * 条件查询下载产品
	 * (non-Javadoc)
	 * @see com.infonautics.dao.IDownloadDao#doQueryList(int, int, java.lang.String)
	 */
	@Override
	public PageBean<Download> doQueryList(int pageSize, int page, String sql)
			throws Exception {
		
		final String hql = "from Download where 1=1 "+sql+" order by id desc";
		int allRow = downloadDao.getAllRowCount("select count(*) from Download where 1=1 "+sql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int length = pageSize;
		final int currentPage = PageBean.countCurrentPage(page);
		
		List<Download> list = downloadDao.getObjects(hql, offset, length);
		
		PageBean<Download> pageBean = new PageBean<Download>();
		
		pageBean.setAllRow(allRow);
		pageBean.setCurrentPage(currentPage);
		pageBean.setList(list);
		pageBean.setPageSize(pageSize);
		pageBean.setTotalPage(totalPage);
		
		return pageBean;
	}

	/*
	 * 
	 * (non-Javadoc)
	 * @see com.system.dao.ISystemDao#doList(int, int, int)
	 */
	@Override
	public PageBean<Download> doList(int pageSize, int page, int id)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * 添加下载产品
	 * (non-Javadoc)
	 * @see com.system.dao.ISystemDao#doAdd(java.lang.Object)
	 */
	@Override
	public void doAdd(Download object) throws Exception {
		
		if(object != null){
			downloadDao.addObject(object);
		}
	}

	/*
	 * 修改下载产品
	 * (non-Javadoc)
	 * @see com.system.dao.ISystemDao#doModfiy(java.lang.Object)
	 */
	@Override
	public void doModfiy(Download object) throws Exception {
		
		if(object != null){
			downloadDao.updateObject(object);
		}
	}

	/*
	 * 删除下载产品
	 * (non-Javadoc)
	 * @see com.system.dao.ISystemDao#doDelete(int)
	 */
	@Override
	public void doDelete(int id) throws Exception {
		
		downloadDao.deleteObject(id, Download.class);
	}

	/*
	 * 根据id查询产品
	 * (non-Javadoc)
	 * @see com.system.dao.ISystemDao#doOne(int)
	 */
	@Override
	public Download doOne(int id) throws Exception {
		
		return downloadDao.getObject(id, Download.class);
	}

}
